(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('MyVoteDialogController', MyVoteDialogController);

    MyVoteDialogController.$inject = ['$timeout', '$scope','$http', '$translate','$stateParams', '$uibModalInstance', 'entity', 'MyVote'];

    function MyVoteDialogController ($timeout, $scope,$http,$translate, $stateParams, $uibModalInstance, entity, MyVote) {
        var vm = this;
        vm.vote={};
        vm.vote.questionnaire = entity;
        vm.vote.candidates={};
        vm.candidates=[{content:''}];
        vm.vote.questionnaire.single=1;
        vm.clear = clear;
        vm.datePickerOpenStatus = {};
        vm.openCalendar = openCalendar;
        vm.save = save;
        vm.vote.questionnaire.endAt=new Date();
        vm.categories=[];
        vm.buttonBar={
        		
        		show: true,
                now: {
                    show: false,
                    text: 'Now!'
                },
                today: {
                    show: true,
                    text: 'Today!'
                },
                clear: {
                    show: false,
                    text: 'Wipe'
                },
                date: {
                    show: true,
                    text: 'Date'
                },
                time: {
                    show: true,
                    text: 'Time'
                },
                close: {
                    show: true,
                    text:'关闭'
                }
        };
        vm.datepickerOptions= {
            	minDate:new Date(),
            	maxDate: null
            }
        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            vm.vote.candidates=vm.candidates;
            if (vm.vote.questionnaire.id !== null) {
            	MyVote.update(vm.vote, onSaveSuccess, onSaveError);
            } else {
            	MyVote.save(vm.vote, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('milkbeeApp:questionnaireUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }

        vm.datePickerOpenStatus.createdAt = false;
        vm.datePickerOpenStatus.endAt = false;

        function openCalendar (date) {
            vm.datePickerOpenStatus[date] = true;
        }
        
        vm.addCandidate=function(){
        	var item={content:''};
        	vm.candidates.push(item);
        }
        
        function loadCategores(){
        	$http({
        		method: 'get',
        		url: '/api/categoriesbylevel/1',
        		transformRequest: angular.identity
        	}).success(function(ret){
        		vm.categories=ret;
        		vm.vote.questionnaire.categoryId=vm.categories[0].id;
        		vm.category=vm.categories[0];
        	});
        }
        this.categoryChange=function (obj){
        	//vm.category.parentId=vm.parentCategory.id;
        	vm.vote.questionnaire.categoryId=vm.category.id;
        }  
        loadCategores();
    }
})();
